package com.woniu.p2p.config;

import com.woniu.p2p.factory.FilterChainDefinitionMapBuilder;
import com.woniu.p2p.filter.MyRolesAuthorizationFilter;
import com.woniu.p2p.realms.ShiroRealm;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Bean(name = "securityManager")
    public DefaultWebSecurityManager defaultWebSecurityManager(){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setCacheManager(ehCacheManager());
        defaultWebSecurityManager.setRealm(shiroRealm());
        return defaultWebSecurityManager;
    }

    @Bean
    public EhCacheManager ehCacheManager(){
        EhCacheManager ehCacheManager = new EhCacheManager();
        return ehCacheManager;
    }

    @Bean(name = "jdbcRealm")
    public ShiroRealm shiroRealm(){
        ShiroRealm shiroRealm = new ShiroRealm();
        shiroRealm.setCredentialsMatcher(credentialsMatcher());
        return shiroRealm;
    }

    @Bean
    public CredentialsMatcher credentialsMatcher(){
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        hashedCredentialsMatcher.setHashAlgorithmName("MD5");
        hashedCredentialsMatcher.setHashIterations(1024);
        return hashedCredentialsMatcher;
    }


    @Bean
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor(){
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    @DependsOn("lifecycleBeanPostProcessor")
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
        return new DefaultAdvisorAutoProxyCreator();
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(){
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(defaultWebSecurityManager());
        return authorizationAttributeSourceAdvisor;
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager());
        shiroFilterFactoryBean.setLoginUrl("/login");//设置登录的url:未登录时将自动跳转自该界面
        shiroFilterFactoryBean.setSuccessUrl("/index");//设置登录成功后跳转的url
        shiroFilterFactoryBean.setUnauthorizedUrl("/login");//设置未进行认证时跳转的url;
        Map<String, Filter> map = new HashMap<>();
        map.put("roles",myRolesAuthorizationFilter());//value为自定义过滤器类的全限定名
        shiroFilterFactoryBean.setFilters(map);//设置自定义过滤器
        shiroFilterFactoryBean.setFilterChainDefinitionMap(getMap());
        return shiroFilterFactoryBean;
    }


    //自定义过滤器
    @Bean
    public MyRolesAuthorizationFilter myRolesAuthorizationFilter(){
        return new MyRolesAuthorizationFilter();
    }

    //路径权限注入工厂bean
    @Bean
    public FilterChainDefinitionMapBuilder filterChainDefinitionMapBuilder(){
        return new FilterChainDefinitionMapBuilder();
    }

    @Bean
    public Map<String,String> getMap(){
        return filterChainDefinitionMapBuilder().filterChainDefinitionMapBuilder();
    }

}

